package com.netease.dao;

import com.netease.Product;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by meichen on 2018/3/9.
 */
@Repository("ProductDao")
public class ProductDaoImpl implements ProductDao {
    @Autowired
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public void insert(Product product){
        Session session = sessionFactory.openSession();
        try{
            Transaction t = session.beginTransaction();
            session.save(product);
            t.commit();
        }catch (HibernateException e){
            e.printStackTrace();
        }finally {
            session.close();
        }
    };

    public List<Product> getAllProdcut(){
        Session session = sessionFactory.openSession();
        List<Product> productList = new ArrayList<Product>();
        try{
            String hql = "from " + Product.class.getName();
            Query<Product> query = session.createQuery(hql);
            productList = query.getResultList();
        }catch (HibernateException e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return productList;
    }

    public void updateSell(int id){
        Session session = sessionFactory.openSession();
        Transaction t = session.beginTransaction();
        try{
            String uhql = "update " + Product.class.getName() + " s set s.sold = ? where s.id = ?";
            Query query = session.createQuery(uhql);
            query.setParameter(0,"Yes");
            query.setParameter(1,id);
            query.executeUpdate();
        }catch (HibernateException e){
            e.printStackTrace();
        }finally {
            t.commit();
            session.close();
        }
    }

    public void updateEdit(Product p){
        Session session = sessionFactory.openSession();
        Transaction t = session.beginTransaction();
        try{
            String uhql = "update " + Product.class.getName() + " s set s.title = ?, s.summary = ?, s.image = ?, s.detail = ?, s.price = ?, s.sold = ? where s.id = ?";
            Query query = session.createQuery(uhql);
            query.setParameter(0,p.getTitle());
            query.setParameter(1,p.getSummary());
            query.setParameter(2,p.getImage());
            query.setParameter(3,p.getDetail());
            query.setParameter(4,p.getPrice());
            query.setParameter(5,p.getSold());
            query.setParameter(6,p.getId());
            query.executeUpdate();
        }catch (HibernateException e){
            e.printStackTrace();
        }finally {
            t.commit();
            session.close();
        }
    }

    public void delete(Product product){
        Session session = sessionFactory.openSession();
        Transaction t = session.beginTransaction();
        try{
            session.delete(product);
            t.commit();
        }catch (HibernateException e){
            e.printStackTrace();
        }finally {
            session.close();
        }
    }
}
